<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>String-searching algorithm</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/String-searching_algorithm"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-String-searching_algorithm rootpage-String-searching_algorithm skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">String-searching algorithm</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>A <b>string-searching algorithm</b>, sometimes called <b>string-matching algorithm</b>, is an <a href="Algorithm" title="Algorithm">algorithm</a> that searches a body of <a href="String_(computer_science)" title="String (computer science)">text</a> for portions that match by pattern.
</p><p>A basic example of string searching is when the pattern and the searched text are <a href="Array_data_structure" class="mw-redirect" title="Array data structure">arrays</a> of elements of an <a href="Alphabet_(computer_science)" class="mw-redirect" title="Alphabet (computer science)">alphabet</a> (<a href="Finite_set" title="Finite set">finite set</a>) Σ. Σ may be a human language alphabet, for example, the letters <i>A</i> through <i>Z</i> and other applications may use a <i>binary alphabet</i> (Σ = {0,1}) or a <i>DNA alphabet</i> (Σ = {A,C,G,T}) in <a href="Bioinformatics" title="Bioinformatics">bioinformatics</a>.
</p><p>In practice, the method of feasible string-search algorithm may be affected by the string encoding. In particular, if a <a href="Variable-width_encoding" title="Variable-width encoding">variable-width encoding</a> is in use, then it may be slower to find the <i>N</i>th character, perhaps requiring time proportional to <i>N</i>. This may significantly slow some search algorithms. One of many possible solutions is to search for the sequence of code units instead, but doing so may produce false matches unless the encoding is specifically designed to avoid it.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Overview">Overview</h2></div>
<p>The most basic case of string searching involves one (often very long) string, sometimes called the <i>haystack</i>, and one (often very short) string, sometimes called the <i>needle</i>. The goal is to find one or more occurrences of the needle within the haystack. For example, one might search for <i>to</i> within:
</p>
<pre>Some books are to be tasted, others to be swallowed, and some few to be chewed and digested.
</pre>
<p>One might request the first occurrence of "to", which is the fourth word; or all occurrences, of which there are 3; or the last, which is the fifth word from the end.
</p><p>Very commonly, however, various constraints are added. For example, one might want to match the "needle" only where it consists of one (or more) complete words—perhaps defined as <i>not</i> having other letters immediately adjacent on either side. In that case a search for "hew" or "low" should fail for the example sentence above, even though those literal strings do occur.
</p><p>Another common example involves "normalization". For many purposes, a search for a phrase such as "to be" should succeed even in places where there is something else intervening between the "to" and the "be":
</p>
<ul><li>More than one space</li>
<li>Other "whitespace" characters such as tabs, non-breaking spaces, line-breaks, etc.</li>
<li>Less commonly, a hyphen or soft hyphen</li>
<li>In structured texts, <a href="Markup_language" title="Markup language">tags</a> or even arbitrarily large but "parenthetical" things such as footnotes, list-numbers or other markers, embedded images, and so on.</li></ul>
<p>Many symbol systems include characters that are synonymous (at least for some purposes):
</p>
<ul><li>Latin-based alphabets distinguish lower-case from upper-case, but for many purposes string search is expected to ignore the distinction.</li>
<li>Many languages include <a href="Typographic_ligature" class="mw-redirect" title="Typographic ligature">ligatures</a>, where one composite character is equivalent to two or more other characters.</li>
<li>Many writing systems involve <a href="Diacritical_marks" class="mw-redirect" title="Diacritical marks">diacritical marks</a> such as accents or <a href="Vowel_pointing_(disambiguation)" class="mw-redirect mw-disambig" title="Vowel pointing (disambiguation)">vowel points</a>, which may vary in their usage, or be of varying importance in matching.</li>
<li>DNA sequences can involve <a href="Non-coding" class="mw-redirect" title="Non-coding">non-coding</a> segments which may be ignored for some purposes, or polymorphisms that lead to no change in the encoded proteins, which may not count as a true difference for some other purposes.</li>
<li>Some languages have rules where a different character or form of character must be used at the start, middle, or end of words.</li></ul>
<p>Finally, for strings that represent natural language, aspects of the language itself become involved. For example, one might wish to find all occurrences of a "word" despite it having alternate spellings, prefixes or suffixes, etc.
</p><p>Another more complex type of search is <a href="Regular_expression" title="Regular expression">regular expression</a> searching, where the user constructs a pattern of characters or other symbols, and any match to the pattern should fulfill the search. For example, to catch both the American English word "color" and the British equivalent "colour", instead of searching for two different literal strings, one might use a regular expression such as:
</p>
<pre>colou?r
</pre>
<p>where the "?" conventionally makes the preceding character ("u") optional.
</p><p>This article mainly discusses algorithms for the simpler kinds of string searching.
</p><p>A similar problem introduced in the field of bioinformatics and genomics is the maximal exact matching (MEM).<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> Given two strings, MEMs are common substrings that cannot be extended left or right without causing a mismatch.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Examples_of_search_algorithms">Examples of search algorithms</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Naive_string_search">Naive string search</h3></div>
<p>A simple and inefficient way to see where one string occurs inside another is to check at each index, one by one. First, we see if there is a copy of the needle starting at the first character of the haystack; if not, we look to see if there's a copy of the needle starting at the second character of the haystack, and so forth. In the normal case, we only have to look at one or two characters for each wrong position to see that it is a wrong position, so in the average case, this takes <a href="Big_O_notation" title="Big O notation">O</a>(<i>n</i> + <i>m</i>) steps, where <i>n</i> is the length of the haystack and <i>m</i> is the length of the needle; but in the worst case, searching for a string like "aaaab" in a string like "aaaaaaaaab", it takes <a href="Big_O_notation" title="Big O notation">O</a>(<i>nm</i>)
</p>
<div class="mw-heading mw-heading3"><h3 id="Finite-state-automaton-based_search">Finite-state-automaton-based search</h3></div>
<p>In this approach, backtracking is avoided by constructing a <a href="Deterministic_finite_automaton" title="Deterministic finite automaton">deterministic finite automaton</a> (DFA) that recognizes a stored search string. These are expensive to construct—they are usually created using the <a href="Powerset_construction" title="Powerset construction">powerset construction</a>—but are very quick to use. For example, the <a href="Deterministic_finite_automaton" title="Deterministic finite automaton">DFA</a> shown to the right recognizes the word "MOMMY". This approach is frequently generalized in practice to search for arbitrary <a href="Regular_expression" title="Regular expression">regular expressions</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Stubs">Stubs</h3></div>
<p><a href="Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" title="Knuth–Morris–Pratt algorithm">Knuth–Morris–Pratt</a> computes a <a href="Deterministic_finite_automaton" title="Deterministic finite automaton">DFA</a> that recognizes inputs with the string to search for as a suffix, <a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer–Moore</a> starts searching from the end of the needle, so it can usually jump ahead a whole needle-length at each step. Baeza–Yates keeps track of whether the previous <i>j</i> characters were a prefix of the search string, and is therefore adaptable to <a href="Fuzzy_string_searching" class="mw-redirect" title="Fuzzy string searching">fuzzy string searching</a>. The <a href="Bitap_algorithm" title="Bitap algorithm">bitap algorithm</a> is an application of Baeza–Yates' approach.
</p>
<div class="mw-heading mw-heading3"><h3 id="Index_methods">Index methods</h3></div>
<p>Faster search algorithms preprocess the text. After building a <a href="Substring_index" title="Substring index">substring index</a>, for example a <a href="Suffix_tree" title="Suffix tree">suffix tree</a> or <a href="Suffix_array" title="Suffix array">suffix array</a>, the occurrences of a pattern can be found quickly. As an example, a suffix tree can be built in <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \Theta (n)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi mathvariant="normal">Θ<!-- Θ --></mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \Theta (n)}</annotation>
</semantics>
</math></span><img src="./a6351206e27071559aa4472579095994f650d76b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.012ex; height:2.843ex;" alt="{\displaystyle \Theta (n)}" loading="lazy"></span> time, and all <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle z}</annotation>
</semantics>
</math></span><img src="./bf368e72c009decd9b6686ee84a375632e11de98.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.088ex; height:1.676ex;" alt="{\displaystyle z}" loading="lazy"></span> occurrences of a pattern can be found in <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(m)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>m</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(m)}</annotation>
</semantics>
</math></span><img src="./a0ffd498cf521ce19814e6b7053f1f8ebb1d3c88.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.623ex; height:2.843ex;" alt="{\displaystyle O(m)}" loading="lazy"></span> time under the assumption that the alphabet has a constant size and all inner nodes in the suffix tree know what leaves are underneath them. The latter can be accomplished by running a <a href="Depth-first_search" title="Depth-first search">DFS algorithm</a> from the root of the suffix tree.
</p>
<div class="mw-heading mw-heading3"><h3 id="Other_variants">Other variants</h3></div>
<p>Some search methods, for instance <a href="Trigram_search" title="Trigram search">trigram search</a>, are intended to find a "closeness" score between the search string and the text rather than a "match/non-match". These are sometimes called <a href="Approximate_string_matching" title="Approximate string matching">"fuzzy" searches</a>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Classification_of_search_algorithms">Classification of search algorithms</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Classification_by_a_number_of_patterns">Classification by a number of patterns</h3></div>
<p>The various <a href="Algorithm" title="Algorithm">algorithms</a> can be classified by the number of patterns each uses.
</p>
<div class="mw-heading mw-heading4"><h4 id="Single-pattern_algorithms">Single-pattern algorithms</h4></div>
<p>In the following compilation, <i>m</i> is the length of the pattern, <i>n</i> the length of the searchable text, and <i>k</i> = |Σ| is the size of the alphabet.
</p>
<table class="wikitable">
<tbody><tr>
<th>Algorithm
</th>
<th>Preprocessing time
</th>
<th>Matching time<sup class=" nourlexpansion citation" id="ref_Asymptotic_times"><a class="external autonumber external" href="https://en.wikipedia.org/wiki/String-searching_algorithm#endnote_Asymptotic_times">[1]</a></sup>
</th>
<th>Space
</th></tr>
<tr>
<th>Naïve algorithm
</th>
<td>none
</td>
<td>Θ(n+m) in average,<br> O(mn)
</td>
<td>none
</td></tr>
<tr>
<th>Automaton-based matching
</th>
<td>Θ(km)
</td>
<td>Θ(n)
</td>
<td>Θ(km)
</td></tr>
<tr>
<th><a href="Rabin%E2%80%93Karp_algorithm" title="Rabin–Karp algorithm">Rabin–Karp</a>
</th>
<td>Θ(m)
</td>
<td>Θ(n) in average,<br> O(mn) at worst
</td>
<td>O(1)
</td></tr>
<tr>
<th><a href="Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" title="Knuth–Morris–Pratt algorithm">Knuth–Morris–Pratt</a>
</th>
<td>Θ(m)
</td>
<td>Θ(n)
</td>
<td>Θ(m)
</td></tr>
<tr>
<th><a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer–Moore</a>
</th>
<td>Θ(m + k)
</td>
<td>O(n/m) at best,<br> O(mn) at worst
</td>
<td>Θ(k)
</td></tr>
<tr>
<th><a href="Two-way_string-matching_algorithm" title="Two-way string-matching algorithm">Two-way algorithm</a><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup class=" nourlexpansion citation" id="ref_libc"><a class="external autonumber external" href="https://en.wikipedia.org/wiki/String-searching_algorithm#endnote_libc">[2]</a></sup>
</th>
<td>Θ(m)
</td>
<td>O(n)
</td>
<td>O(log(m))
</td></tr>
<tr>
<th>Backward Non-Deterministic <a href="Suffix_automaton" title="Suffix automaton">DAWG</a> Matching (BNDM)<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup class=" nourlexpansion citation" id="ref_fuzzy+regexp"><a class="external autonumber external" href="https://en.wikipedia.org/wiki/String-searching_algorithm#endnote_fuzzy+regexp">[3]</a></sup>
</th>
<td>O(m)
</td>
<td>Ω(n/m) at best,<br> O(mn) at worst
</td>
<td>
</td></tr>
<tr>
<th>Backward Oracle Matching (BOM)<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</th>
<td>O(m)
</td>
<td>O(mn)
</td>
<td>
</td></tr></tbody></table>
<dl><dd>1.<style data-mw-deduplicate="TemplateStyles:r1041539562">
/* start https://en.wikipedia.org/ */
.mw-parser-output .citation{word-wrap:break-word}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}
/* end https://en.wikipedia.org/ */
</style><span class="citation wikicite" id="endnote_Asymptotic_times"><b><a href="#ref_Asymptotic_times">^</a></b></span> Asymptotic times are expressed using <a href="Big_O_notation" title="Big O notation">O, Ω, and Θ notation</a>.</dd>
<dd>2.<span class="citation wikicite" id="endnote_libc"><b><a href="#ref_libc">^</a></b></span> Used to implement the <i>memmem</i> and <a href="C_string_handling#Functions" title="C string handling"><i>strstr</i></a> search functions in the <a href="Glibc" title="Glibc">glibc</a><sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> and <a href="Musl" title="Musl">musl</a><sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> <a href="C_standard_libraries" class="mw-redirect" title="C standard libraries">C standard libraries</a>.</dd>
<dd>3.<span class="citation wikicite" id="endnote_fuzzy+regexp"><b><a href="#ref_fuzzy+regexp">^</a></b></span> Can be extended to handle <a href="Approximate_string_matching" title="Approximate string matching">approximate string matching</a> and (potentially-infinite) sets of patterns represented as <a href="Regular_language" title="Regular language">regular languages</a>.</dd></dl>
<p>The <b><a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer–Moore string-search algorithm</a></b> has been the standard benchmark for the practical string-search literature.<sup id="cite_ref-:0_8-0" class="reference"><a href="#cite_note-:0-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading4"><h4 id="Algorithms_using_a_finite_set_of_patterns">Algorithms using a finite set of patterns</h4></div>
<p>In the following compilation, <i>M</i> is the length of the longest pattern, <i>m</i> their total length, <i>n</i> the length of the searchable text, <i>o</i> the number of occurrences.
</p>
<table class="wikitable">
<tbody><tr>
<th>Algorithm
</th>
<th>Extension of
</th>
<th>Preprocessing time
</th>
<th>Matching time<sup class=" nourlexpansion citation" id="ref_Asymptotic_times"><a class="external autonumber external" href="https://en.wikipedia.org/wiki/String-searching_algorithm#endnote_Asymptotic_times">[4]</a></sup>
</th>
<th>Space
</th></tr>
<tr>
<th><a href="Aho%E2%80%93Corasick_algorithm" title="Aho–Corasick algorithm">Aho–Corasick</a>
</th>
<td><a href="Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" title="Knuth–Morris–Pratt algorithm">Knuth–Morris–Pratt</a>
</td>
<td>Θ(m)
</td>
<td>Θ(n + o)
</td>
<td>Θ(m)
</td></tr>
<tr>
<th><a href="Commentz-Walter_algorithm" title="Commentz-Walter algorithm">Commentz-Walter</a>
</th>
<td><a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer-Moore</a>
</td>
<td>Θ(m)
</td>
<td>Θ(M * n) worst case <br> sublinear in average<sup id="cite_ref-Commentz-Walter_9-0" class="reference"><a href="#cite_note-Commentz-Walter-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</td>
<td>Θ(m)
</td></tr>
<tr>
<th>Set-BOM
</th>
<td>Backward Oracle Matching
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr></tbody></table>
<div class="mw-heading mw-heading4"><h4 id="Algorithms_using_an_infinite_number_of_patterns">Algorithms using an infinite number of patterns</h4></div>
<p>Naturally, the patterns can not be enumerated finitely in this case. They are represented usually by a <a href="Regular_grammar" title="Regular grammar">regular grammar</a> or <a href="Regular_expression" title="Regular expression">regular expression</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Classification_by_the_use_of_preprocessing_programs">Classification by the use of preprocessing programs</h3></div>
<p>Other classification approaches are possible. One of the most common uses preprocessing as main criteria.
</p>
<table class="wikitable">
<caption>Classes of string searching algorithms<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup>
</caption>
<tbody><tr>
<th>
</th>
<th>Text not preprocessed
</th>
<th>Text preprocessed
</th></tr>
<tr>
<th>Patterns not preprocessed
</th>
<td>Elementary algorithms
</td>
<td>Index methods
</td></tr>
<tr>
<th>Patterns preprocessed
</th>
<td>Constructed search engines
</td>
<td>Signature methods<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</td></tr></tbody></table>
<div class="mw-heading mw-heading3"><h3 id="Classification_by_matching_strategies">Classification by matching strategies</h3></div>
<p>Another one classifies the algorithms by their matching strategy:<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li>Match the prefix first (Knuth–Morris–Pratt, Shift-And, Aho–Corasick)</li>
<li>Match the suffix first (Boyer–Moore and variants, Commentz-Walter)</li>
<li>Match the best factor first (BNDM, BOM, Set-BOM)</li>
<li>Other strategy (Naïve, Rabin–Karp, Vectorized)</li></ul>
<div class="mw-heading mw-heading3"><h3 id="Real-time_string_matching">Real-time string matching</h3></div>
<p>In real-time string matching, one requires the matcher to output a response after reading each character of the text, that indicates
whether this is the last character of a match. The response has to be given within constant time.
The requirement regarding preprocessing vary: O(<i>m</i>) preprocessing may be allowed after the pattern is read (but before the reading of
the text), or a stricter requirement may be posed according to which the matcher has to also pause for at most a constant time after reading any character of the pattern (including the last).
For the more lenient version, if one does not mind that the preprocessing time and memory requirement dependend on the size of the alphabet, a real-time solution is provided by
automaton matching.
<a href="Zvi_Galil" title="Zvi Galil">Zvi Galil</a> developed a method to turn certain algorithms into real-time algorithms, and applied it to produce a variant of the KMP
matcher that runs in real time under the strict requirement.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="String_searching_with_don't_cares">String searching with don't cares</h2></div>
<p>In this version of the string searching problem, there is a special symbol, ø (read: don't care), which can match any other symbol (including another ø).
Don't care symbols can appear either in the pattern or in the text. In 2002, an algorithm for this problem that runs in <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(n\log m)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mi>log</mi>
<mo><!-- --></mo>
<mi>m</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(n\log m)}</annotation>
</semantics>
</math></span><img src="./682cdba436d9a0d65dba5410e350bb5670a005b6.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:10.764ex; height:2.843ex;" alt="{\displaystyle O(n\log m)}" loading="lazy"></span> time has been
given by <a href="Richard_Cole" title="Richard Cole">Richard Cole</a> and Ramesh Hariharan, improving on a solution from 1973 by <a href="Michael_J._Fischer" title="Michael J. Fischer">Fischer</a> and <a href="Michael_S._Paterson" class="mw-redirect" title="Michael S. Paterson">Paterson</a>
that has complexity <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(n\log m\log k)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mi>log</mi>
<mo><!-- --></mo>
<mi>m</mi>
<mi>log</mi>
<mo><!-- --></mo>
<mi>k</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(n\log m\log k)}</annotation>
</semantics>
</math></span><img src="./84a82d2cd6255b3162905a42debe14c6cdc30c78.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:15.721ex; height:2.843ex;" alt="{\displaystyle O(n\log m\log k)}" loading="lazy"></span>, where <i>k</i> is the size of the alphabet.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> Another algorithm, claimed simpler, has been proposed by Clifford and Clifford.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Sequence_alignment" title="Sequence alignment">Sequence alignment</a></li>
<li><a href="Graph_matching" title="Graph matching">Graph matching</a></li>
<li><a href="Pattern_matching" title="Pattern matching">Pattern matching</a></li>
<li><a href="Compressed_pattern_matching" title="Compressed pattern matching">Compressed pattern matching</a></li>
<li><a href="Matching_wildcards" title="Matching wildcards">Matching wildcards</a></li>
<li><a href="Approximate_string_matching" title="Approximate string matching">Approximate string matching</a></li>
<li><a href="Full-text_search" title="Full-text search">Full-text search</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFKurtzPhillippyDelcherSmoot2004" class="citation journal cs1">Kurtz, Stefan; Phillippy, Adam; Delcher, Arthur L; Smoot, Michael; Shumway, Martin; Antonescu, Corina; Salzberg, Steven L (2004). <a rel="nofollow" class="external text" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC395750">"Versatile and open software for comparing large genomes"</a>. <i>Genome Biology</i>. <b>5</b> (2): R12. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://doi.org/10.1186%2Fgb-2004-5-2-r12">10.1186/gb-2004-5-2-r12</a></span>. <a href="ISSN_(identifier)" class="mw-redirect" title="ISSN (identifier)">ISSN</a> <a rel="nofollow" class="external text" href="https://search.worldcat.org/issn/1465-6906">1465-6906</a>. <a href="PMC_(identifier)" class="mw-redirect" title="PMC (identifier)">PMC</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC395750">395750</a></span>. <a href="PMID_(identifier)" class="mw-redirect" title="PMID (identifier)">PMID</a> <a rel="nofollow" class="external text" href="https://pubmed.ncbi.nlm.nih.gov/14759262">14759262</a>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFKhanBloomKruglyakSingh2009" class="citation journal cs1">Khan, Zia; Bloom, Joshua S.; Kruglyak, Leonid; Singh, Mona (2009-07-01). <a rel="nofollow" class="external text" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2732316">"A practical algorithm for finding maximal exact matches in large sequence datasets using sparse suffix arrays"</a>. <i>Bioinformatics</i>. <b>25</b> (13): <span class="nowrap">1609–</span>1616. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1093%2Fbioinformatics%2Fbtp275">10.1093/bioinformatics/btp275</a>. <a href="PMC_(identifier)" class="mw-redirect" title="PMC (identifier)">PMC</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2732316">2732316</a></span>. <a href="PMID_(identifier)" class="mw-redirect" title="PMID (identifier)">PMID</a> <a rel="nofollow" class="external text" href="https://pubmed.ncbi.nlm.nih.gov/19389736">19389736</a>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="CITEREFCrochemorePerrin1991" class="citation journal cs1">Crochemore, Maxime; Perrin, Dominique (1 July 1991). <a rel="nofollow" class="external text" href="http://monge.univ-mlv.fr/~mac/Articles-PDF/CP-1991-jacm.pdf">"Two-way string-matching"</a> <span class="cs1-format">(PDF)</span>. <i>Journal of the ACM</i>. <b>38</b> (3): <span class="nowrap">650–</span>674. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F116825.116845">10.1145/116825.116845</a>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:15055316">15055316</a>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20211124025145/http://monge.univ-mlv.fr/~mac/Articles-PDF/CP-1991-jacm.pdf">Archived</a> <span class="cs1-format">(PDF)</span> from the original on 24 November 2021<span class="reference-accessdate">. Retrieved <span class="nowrap">5 April</span> 2019</span>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFNavarroRaffinot1998" class="citation book cs1">Navarro, Gonzalo; Raffinot, Mathieu (1998). <a rel="nofollow" class="external text" href="https://users.dcc.uchile.cl/~gnavarro/ps/cpm98.pdf">"A bit-parallel approach to suffix automata: Fast extended string matching"</a> <span class="cs1-format">(PDF)</span>. <i>Combinatorial Pattern Matching</i>. Lecture Notes in Computer Science. Vol. 1448. Springer Berlin Heidelberg. pp. <span class="nowrap">14–</span>33. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2Fbfb0030778">10.1007/bfb0030778</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-3-540-64739-3</bdi>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20190105101910/https://users.dcc.uchile.cl/~gnavarro/ps/cpm98.pdf">Archived</a> <span class="cs1-format">(PDF)</span> from the original on 2019-01-05<span class="reference-accessdate">. Retrieved <span class="nowrap">2019-11-22</span></span>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite id="CITEREFFanYaoMa2009" class="citation book cs1">Fan, H.; Yao, N.; Ma, H. (December 2009). <a rel="nofollow" class="external text" href="https://pdfs.semanticscholar.org/8d81/94c293f8a81394ba545d09bd6ec711ad4c17.pdf">"Fast Variants of the Backward-Oracle-Marching Algorithm"</a> <span class="cs1-format">(PDF)</span>. <i>2009 Fourth International Conference on Internet Computing for Science and Engineering</i>. pp. <span class="nowrap">56–</span>59. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FICICSE.2009.53">10.1109/ICICSE.2009.53</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-4244-6754-9</bdi>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:6073627">6073627</a>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20220510030242/https://www.semanticscholar.org/paper/Efficient-Variants-of-the-Backward-Oracle-Matching-Faro-Lecroq/d24e4bad432e5b88b4fb752ba868e3d3e609a2c4?p2df">Archived</a> from the original on 2022-05-10<span class="reference-accessdate">. Retrieved <span class="nowrap">2019-11-22</span></span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://code.woboq.org/userspace/glibc/string/str-two-way.h.html">"glibc/string/str-two-way.h"</a>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20200920180414/https://code.woboq.org/userspace/glibc/string/str-two-way.h.html">Archived</a> from the original on 2020-09-20<span class="reference-accessdate">. Retrieved <span class="nowrap">2022-03-22</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://git.musl-libc.org/cgit/musl/tree/src/string/memmem.c">"musl/src/string/memmem.c"</a>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20201001184748/http://git.musl-libc.org/cgit/musl/tree/src/string/memmem.c">Archived</a> from the original on 1 October 2020<span class="reference-accessdate">. Retrieved <span class="nowrap">23 November</span> 2019</span>.</cite></span>
</li>
<li id="cite_note-:0-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-:0_8-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFHumeSunday1991" class="citation journal cs1">Hume; Sunday (1991). "Fast String Searching". <i>Software: Practice and Experience</i>. <b>21</b> (11): <span class="nowrap">1221–</span>1248. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1002%2Fspe.4380211105">10.1002/spe.4380211105</a>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:5902579">5902579</a>.</cite></span>
</li>
<li id="cite_note-Commentz-Walter-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-Commentz-Walter_9-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFCommentz-Walter1979" class="citation conference cs1">Commentz-Walter, Beate (1979). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20171010223532/http://www.hs-albsig.de/studium/wirtschaftsinformatik/Documents/commentzwalterextab.pdf"><i>A String Matching Algorithm Fast on the Average</i></a> <span class="cs1-format">(PDF)</span>. <a href="International_Colloquium_on_Automata%2C_Languages_and_Programming" title="International Colloquium on Automata, Languages and Programming">International Colloquium on Automata, Languages and Programming</a>. <a href="Lecture_Notes_in_Computer_Science" title="Lecture Notes in Computer Science">LNCS</a>. Vol. 71. Graz, Austria: Springer. pp. <span class="nowrap">118–</span>132. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2F3-540-09510-1_10">10.1007/3-540-09510-1_10</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>3-540-09510-1</bdi>. Archived from <a rel="nofollow" class="external text" href="http://www.hs-albsig.de/studium/wirtschaftsinformatik/Documents/commentzwalterextab.pdf">the original</a> <span class="cs1-format">(PDF)</span> on 2017-10-10.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text">Melichar, Borivoj, Jan Holub, and J. Polcar. Text Searching Algorithms. Volume I: Forward String Matching. Vol. 1. 2 vols., 2005. <a rel="nofollow" class="external free" href="http://stringology.org/athens/TextSearchingAlgorithms/">http://stringology.org/athens/TextSearchingAlgorithms/</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20160304074815/http://stringology.org/athens/TextSearchingAlgorithms/">Archived</a> 2016-03-04 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a>.</span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite id="CITEREFLitwinMokademRigauxSchwarz2007" class="citation cs2">Litwin, Witold; Mokadem, Riad; Rigaux, Philippe; Schwarz, Thomas (2007), <a rel="nofollow" class="external text" href="https://www.vldb.org/conf/2007/papers/research/p207-litwin.pdf"><i>Fast nGram-Based String Search Over Data Encoded Using Algebraic Signatures</i></a> <span class="cs1-format">(PDF)</span>, <a href="International_Conference_on_Very_Large_Data_Bases" title="International Conference on Very Large Data Bases">International Conference on Very Large Data Bases</a></cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite id="CITEREFGonzalo_NavarroMathieu_Raffinot2008" class="citation cs2">Gonzalo Navarro; Mathieu Raffinot (2008), <i>Flexible Pattern Matching Strings: Practical On-Line Search Algorithms for Texts and Biological Sequences</i>, Cambridge University Press, <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-521-03993-2</bdi></cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite id="CITEREFGalil1981" class="citation journal cs1">Galil, Zvi (1981). "String matching in real time". <i>Journal of the ACM</i>. <b>28</b> (1): <span class="nowrap">134–</span>149. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F322234.322244">10.1145/322234.322244</a>.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite id="CITEREFColeHariharan2002" class="citation conference cs1">Cole, Richard; Hariharan, Ramesh (2002). "Verifying candidate matches in sparse and wildcard matching". <i>Proceedings of the thiry-fourth annual ACM symposium on Theory of computing</i>. pp. <span class="nowrap">592–</span>601.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite id="CITEREFCliffordClifford2007" class="citation journal cs1">Clifford, Peter; Clifford, Raphaël (January 2007). "Simple deterministic wildcard matching". <i>Information Processing Letters</i>. <b>101</b> (2): <span class="nowrap">53–</span>54. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1016%2Fj.ipl.2006.08.002">10.1016/j.ipl.2006.08.002</a>.</cite></span>
</li>
</ol></div>
<ul><li>R. S. Boyer and J. S. Moore, <i><a rel="nofollow" class="external text" href="http://www.cs.utexas.edu/~moore/publications/fstrpos.pdf">A fast string searching algorithm</a>,</i> Carom. ACM 20, (10), 262–272(1977).</li>
<li><a href="Thomas_H._Cormen" title="Thomas H. Cormen">Thomas H. Cormen</a>, <a href="Charles_E._Leiserson" title="Charles E. Leiserson">Charles E. Leiserson</a>, <a href="Ronald_L._Rivest" class="mw-redirect" title="Ronald L. Rivest">Ronald L. Rivest</a>, and <a href="Clifford_Stein" title="Clifford Stein">Clifford Stein</a>. <i><a href="Introduction_to_Algorithms" title="Introduction to Algorithms">Introduction to Algorithms</a></i>, Third Edition. MIT Press and McGraw-Hill, 2009. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-262-03293-7</bdi>. Chapter 32: String Matching, pp. 985–1013.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1290876196">
/* start https://en.wikipedia.org/ */
.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .infobox .side-box{font-size:100%}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1237033735">
/* start https://en.wikipedia.org/ */
@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}
/* end https://en.wikipedia.org/ */
</style><div class="side-box side-box-right sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */
.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}
/* end https://en.wikipedia.org/ */
</style>
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">Wikimedia Commons has media related to <span style="font-weight: bold; font-style: italic;"><a href="https://commons.wikimedia.org/wiki/Category:String-searching_algorithm" class="extiw external" title="commons:Category:String-searching algorithm">String-searching algorithm</a></span>.</div></div>
</div>
<ul><li><a rel="nofollow" class="external text" href="http://www.cs.ucr.edu/%7Estelo/pattern.html">Huge list of pattern matching links</a> Last updated: 12/27/2008 20:18:38</li>
<li><a rel="nofollow" class="external text" href="https://www-igm.univ-mlv.fr/~lecroq/string/index.html">Large (maintained) list of string-matching algorithms</a></li>
<li><a rel="nofollow" class="external text" href="https://xlinux.nist.gov/dads/HTML/stringMatching.html">NIST list of string-matching algorithms</a></li>
<li><a rel="nofollow" class="external text" href="http://johannburkard.de/software/stringsearch/">StringSearch – high-performance pattern matching algorithms in Java</a> – Implementations of many String-Matching-Algorithms in Java (BNDM, Boyer-Moore-Horspool, Boyer-Moore-Horspool-Raita, Shift-Or)</li>
<li><a rel="nofollow" class="external text" href="http://stringsandchars.amygdalum.net/">StringsAndChars</a> – Implementations of many String-Matching-Algorithms (for single and multiple patterns) in Java</li>
<li><a rel="nofollow" class="external text" href="http://www-igm.univ-mlv.fr/~lecroq/string/index.html">Exact String Matching Algorithms</a> — Animation in Java, Detailed description and C implementation of many algorithms.</li>
<li><a rel="nofollow" class="external text" href="http://www.cs.ucr.edu/~stelo/cpm/cpm04/35_Navarro.pdf">(PDF) Improved Single and Multiple Approximate String Matching</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20170311221134/http://www.cs.ucr.edu/~stelo/cpm/cpm04/35_Navarro.pdf">Archived</a> 2017-03-11 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a></li>
<li><a rel="nofollow" class="external text" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2647288/">Kalign2: high-performance multiple alignment of protein and nucleotide sequences allowing external features</a></li>
<li><a rel="nofollow" class="external text" href="https://www.codeproject.com/Articles/5282980/Fastest-Fulltext-Vector-Scalar-Exact-Searcher">NyoTengu – high-performance pattern matching algorithm in C</a> – Implementations of Vector and Scalar String-Matching-Algorithms in C</li>
<li><a rel="nofollow" class="external text" href="https://arxiv.org/html/2502.20338v3">Nathaniel K. Brown, et al.: "KeBaB: k-mer based breaking for finding long MEMs", arXiv:2502.20338v3 (09 Jun 2025).</a></li></ul>
<div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1236075235">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style></div><div role="navigation" class="navbox" aria-labelledby="Strings176" style="padding:3px"><table class="nowraplinks mw-collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div id="Strings176" style="font-size:114%;margin:0 4em"><a href="String_(computer_science)" title="String (computer science)">Strings</a></div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="String_metric" title="String metric">String metric</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Approximate_string_matching" title="Approximate string matching">Approximate string matching</a></li>
<li><a href="Bitap_algorithm" title="Bitap algorithm">Bitap algorithm</a></li>
<li><a href="Damerau%E2%80%93Levenshtein_distance" title="Damerau–Levenshtein distance">Damerau–Levenshtein distance</a></li>
<li><a href="Edit_distance" title="Edit distance">Edit distance</a></li>
<li><a href="Gestalt_pattern_matching" title="Gestalt pattern matching">Gestalt pattern matching</a></li>
<li><a href="Hamming_distance" title="Hamming distance">Hamming distance</a></li>
<li><a href="Jaro%E2%80%93Winkler_distance" title="Jaro–Winkler distance">Jaro–Winkler distance</a></li>
<li><a href="Lee_distance" title="Lee distance">Lee distance</a></li>
<li><a href="Levenshtein_automaton" title="Levenshtein automaton">Levenshtein automaton</a></li>
<li><a href="Levenshtein_distance" title="Levenshtein distance">Levenshtein distance</a></li>
<li><a href="Wagner%E2%80%93Fischer_algorithm" title="Wagner–Fischer algorithm">Wagner–Fischer algorithm </a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Apostolico%E2%80%93Giancarlo_algorithm" title="Apostolico–Giancarlo algorithm">Apostolico–Giancarlo algorithm</a></li>
<li><a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer–Moore string-search algorithm</a></li>
<li><a href="Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm" title="Boyer–Moore–Horspool algorithm">Boyer–Moore–Horspool algorithm</a></li>
<li><a href="Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" title="Knuth–Morris–Pratt algorithm">Knuth–Morris–Pratt algorithm</a></li>
<li><a href="Rabin%E2%80%93Karp_algorithm" title="Rabin–Karp algorithm">Rabin–Karp algorithm</a></li>
<li><a href="Raita_algorithm" title="Raita algorithm">Raita algorithm</a></li>
<li><a href="Trigram_search" title="Trigram search">Trigram search</a></li>
<li><a href="Two-way_string-matching_algorithm" title="Two-way string-matching algorithm">Two-way string-matching algorithm</a></li>
<li><a href="Zhu%E2%80%93Takaoka_string_matching_algorithm" title="Zhu–Takaoka string matching algorithm">Zhu–Takaoka string matching algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Multiple string searching</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Aho%E2%80%93Corasick_algorithm" title="Aho–Corasick algorithm">Aho–Corasick</a></li>
<li><a href="Commentz-Walter_algorithm" title="Commentz-Walter algorithm">Commentz-Walter algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Regular_expression" title="Regular expression">Regular expression</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Comparison_of_regular-expression_engines" class="mw-redirect" title="Comparison of regular-expression engines">Comparison of regular-expression engines</a></li>
<li><a href="Regular_grammar" title="Regular grammar">Regular grammar</a></li>
<li><a href="Thompson's_construction" title="Thompson's construction">Thompson's construction</a></li>
<li><a href="Nondeterministic_finite_automaton" title="Nondeterministic finite automaton">Nondeterministic finite automaton</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Sequence_alignment" title="Sequence alignment">Sequence alignment</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="BLAST_(biotechnology)" title="BLAST (biotechnology)">BLAST</a></li>
<li><a href="Hirschberg's_algorithm" title="Hirschberg's algorithm">Hirschberg's algorithm</a></li>
<li><a href="Needleman%E2%80%93Wunsch_algorithm" title="Needleman–Wunsch algorithm">Needleman–Wunsch algorithm</a></li>
<li><a href="Smith%E2%80%93Waterman_algorithm" title="Smith–Waterman algorithm">Smith–Waterman algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Data_structure" title="Data structure">Data structure</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Deterministic_acyclic_finite_state_automaton" title="Deterministic acyclic finite state automaton">DAFSA</a></li>
<li><a href="Substring_index" title="Substring index">Substring index</a>
<ul><li><a href="Suffix_array" title="Suffix array">Suffix array</a></li>
<li><a href="Suffix_automaton" title="Suffix automaton">Suffix automaton</a></li>
<li><a href="Suffix_tree" title="Suffix tree">Suffix tree</a></li>
<li><a href="Compressed_suffix_array" title="Compressed suffix array">Compressed suffix array</a></li>
<li><a href="LCP_array" title="LCP array">LCP array</a></li>
<li><a href="FM-index" title="FM-index">FM-index</a></li></ul></li>
<li><a href="Generalized_suffix_tree" title="Generalized suffix tree">Generalized suffix tree</a></li>
<li><a href="Rope_(data_structure)" title="Rope (data structure)">Rope</a></li>
<li><a href="Ternary_search_tree" title="Ternary search tree">Ternary search tree</a></li>
<li><a href="Trie" title="Trie">Trie</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Other</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Parsing" title="Parsing">Parsing</a></li>
<li><a href="Pattern_matching" title="Pattern matching">Pattern matching</a></li>
<li><a href="Compressed_pattern_matching" title="Compressed pattern matching">Compressed pattern matching</a></li>
<li><a href="Longest_common_subsequence" title="Longest common subsequence">Longest common subsequence</a></li>
<li><a href="Longest_common_substring" title="Longest common substring">Longest common substring</a></li>
<li><a href="Sequential_pattern_mining" title="Sequential pattern mining">Sequential pattern mining</a></li>
<li>Sorting</li>
<li><a href="Semi-Thue_system" title="Semi-Thue system">String rewriting systems</a></li>
<li><a href="String_operations" title="String operations">String operations</a></li></ul>
</div></td></tr></tbody></table></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-26" href="https://en.wikipedia.org/wiki/?title=String-searching_algorithm&oldid=1302634953">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>